﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Visifire.Gauges;
using System.Windows.Media.Animation;
using CPPEI.Coolzon.Common;
using System.Windows;
using System.Windows.Media;
using Coolzon.UI.Common.Widgets;
using Coolzon.Web.Models;

namespace Coolzon.UI.Charts
{
    public partial class DataGaugeBase : DataWidget
    {
        public DataGaugeBase()
        {
            MajorTicksBackground = new SolidColorBrush(Colors.Black);
            MinorTicksBackground = new SolidColorBrush(Colors.Black);
            CircularScaleLabelsFontSize = 12;

            CircularScaleLabelsFontColor = new SolidColorBrush(Colors.Black);
            CircularScaleLabelsFontFamily = new FontFamily("微软雅黑");

            CapBackground = new SolidColorBrush(Colors.Gray);
            NeedleBackground = new SolidColorBrush(Colors.Orange);

            SetDefaultDataResult();
        }
        protected virtual void SetDefaultDataResult()
        {
            //Random random = new Random();
            //DataSeries[] series = new DataSeries[1];
            //DataItem[] tmpItems = new DataItem[1];
            //for (int i = 1; i <= 1; i++)
            //{
            //    tmpItems[i - 1] = new DataItem();
            //    tmpItems[i - 1].Id = "data" + i;
            //    tmpItems[i - 1].Name = "data" + i;
            //    tmpItems[i - 1].Value = (random.NextDouble() * 120).ToString("F2");
            //}
            //series[0] = new DataSeries();
            //series[0].DataItems = tmpItems;
            //series[0].Id = "series1";
            //series[0].Name = "series1";
            //DataResult = new DataResult();
            //DataResult.DataSeries = series;
        }

        protected override void OnInit(Core.PartContext context)
        {
            base.OnInit(context);

            Gauge gauge = GetGauge();
            if (gauge != null)
            {
                gauge.AnimationEnabled = false;
                SetGauge(gauge);
                SetRange(gauge);
            }
        }

        protected override void OnDataBinding(DataBindingArgs args)
        {
            Gauge gauge = GetGauge();
            //gauge.Indicators.Clear();
            if (args.DataResult.DataSeries.Count == 0)
            {
                return;
            }

            // 只绑定一个串
            var dataSeries = args.DataResult.DataSeries[0];
            // 删除多余的指示器
            if (dataSeries.DataItems.Count < gauge.Indicators.Count)
            {
                for(int index= dataSeries.DataItems.Count; index < gauge.Indicators.Count; )
                {
                    gauge.Indicators.RemoveAt(index);
                }
            }
            for (int itemIndex = 0; itemIndex < dataSeries.DataItems.Count; itemIndex++)
            {
                var dataItem = dataSeries.DataItems[itemIndex];
                Indicator indicator = null;
                if(itemIndex < gauge.Indicators.Count)
                {
                    indicator = gauge.Indicators[itemIndex];
                }
                else
                {
                    switch (GetIndicatorType(dataSeries.Name))
                    {
                        case IndicatorTypes.Needle:
                            indicator = new NeedleIndicator();
                            SetNeedleIndicator((NeedleIndicator)indicator);
                            break;
                        case IndicatorTypes.Marker:
                            indicator = new MarkerIndicator();
                            SetMarkerIndicator((MarkerIndicator)indicator);
                            break;
                        case IndicatorTypes.Bar:
                            indicator = new BarIndicator();
                            SetBarIndicator((BarIndicator)indicator);
                            break;
                        default:
                            indicator = new NeedleIndicator();
                            SetNeedleIndicator((NeedleIndicator)indicator);
                            break;
                    }
                    gauge.Indicators.Add(indicator);
                }

                if(args.Reason == DataBindingReason.Active)
                {
                    indicator.BeginAnimation(NeedleIndicator.ValueProperty, null);
                    indicator.Value = 0;
                    indicator.BeginAnimation(NeedleIndicator.ValueProperty, new DoubleAnimation(0, Convert.ToDouble(dataItem.Value), new Duration(TimeSpan.FromSeconds(XDataAnimationDuration))), HandoffBehavior.SnapshotAndReplace);
                }
                else
                {
                    //indicator.Value = Convert.ToDouble(dataItem.Value);
                    DoubleAnimation animation = new DoubleAnimation(indicator.Value, Convert.ToDouble(dataItem.Value), new Duration(TimeSpan.FromSeconds(XDataAnimationDuration)));
                    indicator.BeginAnimation(NeedleIndicator.ValueProperty, animation, HandoffBehavior.SnapshotAndReplace);
                }
            }
        }

        protected virtual Gauge GetGauge()
        {
            return null;
        }
        internal virtual IndicatorTypes GetIndicatorType(string dataSeriesName)
        {
            return IndicatorTypes.Needle;
        }

        internal enum IndicatorTypes
        {
            Needle,
            Bar,
            Marker,
        }
    }
}
